# Contributor: Yichun Zhang <admin@openresty.com>
# Maintainer: Yichun Zhang <admin@openresty.com>
pkgname="openresty-pcre2"
pkgver="10.42"
pkgrel=0
pkgdesc="Perl-compatible regular expression library for OpenResty"
url="http://www.pcre.org/"
arch="all"
license="BSD"
#depends=""
makedepends="ccache"
subpackages="$pkgname-dev $pkgname-dbg"
source="https://github.com/PCRE2Project/pcre2/releases/download/pcre2-$pkgver/pcre2-$pkgver.tar.gz"
builddir="$srcdir/pcre2-$pkgver/"
options="!tracedeps !fhs"


pcre2_prefix=/usr/local/openresty/pcre2

build() {
    export CC="ccache gcc -fdiagnostics-color=always"
    export CFLAGS="-g -O3"
    export CXXFLAGS=
    export CPPFLAGS=
    export LDFLAGS=

    ./configure \
        --prefix=$pcre2_prefix \
        --libdir=$pcre2_prefix/lib \
        --enable-jit \
        --enable-pcre2grep-jit \
        --disable-bsr-anycrlf \
        --disable-coverage \
        --disable-ebcdic \
        --disable-fuzz-support \
        --disable-jit-sealloc \
        --disable-never-backslash-C \
        --enable-newline-is-lf \
        --enable-pcre2-8 \
        --enable-pcre2-16 \
        --enable-pcre2-32 \
        --enable-pcre2grep-callout \
        --enable-pcre2grep-callout-fork \
        --disable-pcre2grep-libbz2 \
        --disable-pcre2grep-libz \
        --disable-pcre2test-libedit \
        --enable-percent-zt \
        --disable-rebuild-chartables \
        --enable-shared \
        --disable-static \
        --disable-silent-rules \
        --enable-unicode \
        --disable-valgrind

    make -j$JOBS
}

check() {
	:
}

package() {
    make install DESTDIR="$pkgdir"
    rm -rf $pkgdir$pcre2_prefix/bin
    rm -fr $pkgdir$pcre2_prefix/share
}

doc() {
    local gzip=$(command -v pigz || echo gzip)
    depends="$depends_doc"
    pkgdesc="$pkgdesc (documentation)"
    install_if="docs $pkgname=$pkgver-r$pkgrel"

    local i
    for i in doc man info html sgml licenses gtk-doc ri help; do
        if [ -d "$pkgdir$pcre2_prefix/share/$i" ]; then
            mkdir -p "$subpkgdir$pcre2_prefix/share"
            mv "$pkgdir$pcre2_prefix/share/$i" "$subpkgdir$pcre2_prefix/share/"
        fi
    done

    # compress man pages
    local mandir="$subpkgdir"$pcre2_prefix/share/man
    [ -d "$mandir" ] && find "$mandir" -type l \
        -a \( -name \*.[0-8n] -o -name \*.[0-8][a-z]* \) \
        -a \! \( -name '*.gz' -o -name '*.bz2' -o -name '*.xz' \) \
        | while read symlink; do

        ln -s $(readlink $symlink).gz "$symlink".gz
        rm -f "$symlink"
    done
    [ -d "$mandir" ] && find "$mandir" -type f \
        -a \( -name \*.[0-8n] -o -name \*.[0-8][a-z]* \) \
        -a \! \( -name '*.gz' -o -name '*.bz2' -o -name '*.xz' \) \
        -exec stat -c "%i %n" \{\} \+ | while read inode name; do

        # Skip hardlinks removed in last iteration.
        [ -f "$name" ] || continue

        local islink=0
        find "$mandir" -type f -links +1 \
            -a \( -name \*.[0-8n] -o -name \*.[0-8][a-z]* \) \
            -a \! \( -name '*.gz' -o -name '*.bz2' -o -name '*.xz' \) \
            -exec stat -c "%i %n" \{\} \+ | while read linode lname; do
            if [ "$linode" = "$inode" -a "$lname" != "$name" ]; then
                islink=1
                rm -f "$lname"
                ln -s "${name##*/}".gz "$lname".gz
            fi
        done

        [ $islink -eq 0 ] && $gzip -n -9 "$name"
    done

    rm -f "$subpkgdir$pcre2_prefix/share/info/dir"

    # remove if empty, ignore error (not empty)
    rmdir "$pkgdir$pcre2_prefix/share" "$pkgdir$pcre2_prefix" 2>/dev/null || :
}

static() {
    if ! is_function default_static; then
        local i= devpkg

        # search for -dev package matching our prefix
        if [ -z "$depends_static" ]; then
            devpkg="${subpkgname%-libs-static}"
            devpkg="${devpkg%-static}"
            devpkg="$devpkg-dev"
            if subpackages_has "$devpkg"; then
                depends_static="$devpkg"
            fi
        fi

        depends="$depends_static"
        pkgdesc="$pkgdesc (static library)"

        cd "$pkgdir" || return 0
        local libdirs=usr/lib
        [ -d lib/ ] && libdirs="lib/ $libdirs"

        # move *.a static library
        for i in $(find $libdir -name '*.a'); do
            mkdir -p "$subpkgdir"/"${i%/*}"
            mv "$i" "$subpkgdir/$i" || return 1
        done
        return 0
    fi

    default_static
}

sha512sums="
a3db6c5c620775838819be616652e73ce00f5ef5c1f49f559ff3efb51a119d02f01254c5901c1f7d0c47c0ddfcf4313e38d6ca32c35381b8f87f36896d10e6f7  pcre2-10.42.tar.gz
"
